﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace personalplaner.business.extensions
{
	public static class FrameworkElementExtensions
	{
		/// <summary>
		/// Renders the element to a new Visual and returnes a Image conatining the new rednered visual.
		/// This is mainly used for printing controls that are already displayed in a view to avoid exceptions
		/// </summary>
		/// <param name="element"></param>
		/// <returns></returns>
		public static UIElement RenderNewElement(this FrameworkElement element)
		{
			element.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

			var rtb = new RenderTargetBitmap(
				(int)element.DesiredSize.Width,
				(int)element.DesiredSize.Height,
				90,
				90,
				PixelFormats.Default);

			//var rtb = new RenderTargetBitmap(
			//    (int)element.ActualWidth,
			//    (int)element.ActualHeight,
			//    90,
			//    90,
			//    PixelFormats.Default);

			Visual vis = (Visual)element;
			rtb.Render(vis);

			var img = new Image();
			img.Source = rtb;
			img.Stretch = Stretch.None;
			img.Measure(new Size(
				(int)element.ActualWidth,
				(int)element.ActualHeight));

			Size sizeImage = img.DesiredSize;

			img.Arrange(new Rect(new Point(0, 0), sizeImage));

			return img;

			//RenderTargetBitmap rtb2 = new RenderTargetBitmap(
			//  (int)rtb.Width,
			//  (int)rtb.Height,
			//  90,
			//  90,
			//  PixelFormats.Default);
			//rtb2.Render(img);

			//PngBitmapEncoder png = new PngBitmapEncoder();
			//png.Frames.Add(BitmapFrame.Create(rtb2));

			//Stream ms = new MemoryStream();
			//png.Save(ms);

			//ms.Position = 0;

			////System.Drawing.Image retImg =
			////  System.Drawing.Image.FromStream(ms);
			////return retImg;

			//var image = new BitmapImage();
			//image.BeginInit();
			//image.CacheOption = BitmapCacheOption.OnLoad;
			//image.StreamSource = ms;
			//image.EndInit();
			//return image;

			// version 2
			//// Renderbitmap mit den Bildgrößen erstellen
			//RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
			//                                      (int)element.Width,
			//                                      (int)element.Height,
			//                                      96d,
			//                                      96d,
			//                                      PixelFormats.Pbgra32);
			//// Bild rendern
			//renderBitmap.Render(element);

			//System.IO.MemoryStream strm = new System.IO.MemoryStream();

			//PngBitmapEncoder encoder = new PngBitmapEncoder();
			//encoder.Interlace = PngInterlaceOption.On;
			//encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
			//encoder.Save(strm);
			//strm.Seek(0, System.IO.SeekOrigin.Begin);

			//var image = new BitmapImage();
			//image.BeginInit();
			//image.CacheOption = BitmapCacheOption.OnLoad;
			//image.StreamSource = strm;
			//image.EndInit();

			////remove all references
			//image.StreamSource.Dispose();

			//return image;
		}
	}
}
